package org.alex.samples.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.alex.samples.javabean.ExamTestVO;
import org.alex.samples.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/student")
public class ExamStudentController {

	@Autowired
	private IStudentService iStudentService;

	@RequestMapping(value = "/studentExam/{id}")
	public String subjectTest(HttpSession session, @PathVariable Integer id) {
		session.setAttribute("subId", id);
		return "student/studentExam";
	}
	
	@ResponseBody
	@RequestMapping(value = "/examContent", method = RequestMethod.POST)
	public Map<Integer, List<ExamTestVO>> subjectTest(HttpSession session) {
		String userId = (String) session.getAttribute("userId");
		Integer subId = (Integer) session.getAttribute("subId");
		Map<Integer, List<ExamTestVO>> map = iStudentService.getTestListBySubject(subId, userId);
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/submittingExam", method = RequestMethod.POST)
	public String submittingExam(@RequestParam(value = "answer[]")String[] answer,
			@RequestParam(value = "answerId[]")Integer[] answerId, HttpSession session) {
		String userId = (String) session.getAttribute("userId");
		Integer subId = (Integer) session.getAttribute("subId");
		iStudentService.getScoreByStudentAnswer(userId, subId, answer, answerId);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value = "/getTimer", method = RequestMethod.POST)
	public String getTimer(HttpSession session) {
		String userId = (String) session.getAttribute("userId");
		Integer subId = (Integer) session.getAttribute("subId");
		long remainingTime = iStudentService.getRemainingTime(subId, userId);
		session.setAttribute("remainingTime", remainingTime);
		return remainingTime + "";
	}
}
